/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.admin.action;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import etcore.webapp.grouptalk.common.config.Config;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.service.GroupService;
import etcore.webapp.grouptalk.user.object.Member;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.MemberService;
import etcore.webapp.grouptalk.user.service.UserService;
import etcore.webapp.grouptalk.util.DateUtil;
import etcore.webapp.grouptalk.util.MD5;

/**
 * This is an action to create a root account.
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class CreateRootAccountAction extends HttpServlet {

	private static final long serialVersionUID = -1665301909688556977L;
	public static final String ROOT_PWD = "root";
	public static final String ROOT_USER = "root";
	public static final String ROOT_GROUP = "roots";
	public static final String INIT_MSG = "The root account has been successfully created!";

	public void service(HttpServletRequest req, HttpServletResponse res)
			throws java.io.IOException {

		UserService userService = new UserService();
		GroupService groupService = new GroupService();
		User user = userService.findByUserName(ROOT_USER);
		if (user == null) {
			User newUser = new User();
			MD5 md5 = new MD5();
			newUser.setPwd(md5.getMD5ofStr(ROOT_PWD));
			newUser.setUserName(ROOT_USER);

			newUser.setIsLock(new Integer(0));// user's access is
			userService.save(newUser);
		}

		if (groupService.isUsedName(ROOT_GROUP) == 0) {
			Group rootGroup = new Group();
			rootGroup.setName(ROOT_GROUP);
			rootGroup.setOpen(new Integer(0));
			rootGroup.setUp(new Integer(0));
			rootGroup
					.setTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));
			rootGroup.setPic("/group/images/root.gif");
			groupService.save(rootGroup);

			MemberService memberService = new MemberService();
			Member member = new Member();
			member.setLocked(0);
			member.setGid(rootGroup.getId());
			member.setUserName(ROOT_USER);
			member.setMytype("owner");
			member.setApp(new Integer(0));
			member.setMainnum(new Integer(0));
			member.setCreateTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));
			member.setTotalnum(new Integer(0));
			member.setLastTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));
			member.setEnergy(new Integer(0));
			memberService.save(member);

		}
		res.setCharacterEncoding("gb2312");
		res.setContentType("text/html;   charset=gb2312");
		res.getWriter().write(INIT_MSG);

	}
}
